﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace agvcsimulator.GUI
{
    /// <summary>
    /// Interaction logic for UCTowerLamp.xaml
    /// </summary>
    public partial class UCTowerLamp : UserControl
    {
        bool RedTrigger= false;
        bool GreenTrigger=false;
        bool YellowTrigger=false;

        Timer tmrFilck;

        public UCTowerLamp()
        {
            InitializeComponent();

            tmrFilck = new Timer(new TimerCallback(Tick), null, 0, 1000);
        }

        private void RedOff_MouseUp(object sender, MouseButtonEventArgs e)
        {
            LEDRedOn();
        }

        private void RedOn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            LEDRedOff();
        }


        private void GreedOff_MouseUp(object sender, MouseButtonEventArgs e)
        {
            LEDGreenOn();
        }

        private void GreedOn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            LEDGreenOff();
        }

        private void YellowOff_MouseUp(object sender, MouseButtonEventArgs e)
        {
            LEDYellowOn();
        }

        private void YellowOn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            LEDYellowOff();
        }

        void LEDRedOn()
        {
            RedOn.Visibility = Visibility.Visible;
            RedOff.Visibility = Visibility.Hidden;
        }

        void LEDRedOff()
        {
            RedOn.Visibility = Visibility.Hidden;
            RedOff.Visibility = Visibility.Visible;
        }

        
        void LEDGreenOn()
        {
            GreenOn.Visibility = Visibility.Visible;
            GreenOff.Visibility = Visibility.Hidden;
        }

        void LEDGreenOff()
        {
            GreenOn.Visibility = Visibility.Hidden;
            GreenOff.Visibility = Visibility.Visible;

            RedTrigger = false;
        }

        void LEDYellowOn()
        {
            YellowOn.Visibility = Visibility.Visible;
            YellowOff.Visibility = Visibility.Hidden;

            GreenTrigger = false;
        }

        void LEDYellowOff()
        {
            YellowOn.Visibility = Visibility.Hidden;
            YellowOff.Visibility = Visibility.Visible;

            YellowTrigger = false;
        }

        void LEDRedFilck()
        {
            RedTrigger = true;
        }

        void LEDGreenFilck()
        {
            GreenTrigger = true;
        }

        void LEDYellowFilck()
        {
            YellowTrigger = true;
        }

        bool IsRedOn()
        {
            if (RedOn.Visibility == Visibility.Visible)
                return true;
            else
                return false;
        }

        bool IsGreenOn()
        {
            if(GreenOn.Visibility == Visibility.Visible)
                return true;
            else
                return false;
        }

        bool IsYellowOn()
        {
            if(YellowOn.Visibility == Visibility.Visible)
                return true;
            else
                return false;
        }

        public void Tick(object state)
        {
            if (RedTrigger)
            {
                if (IsRedOn())
                    LEDRedOff();
                else
                    LEDRedOn();
            }

            if (GreenTrigger)
            {
                if (IsGreenOn())
                    LEDGreenOff();
                else
                    LEDGreenOn();
            }

            if (YellowTrigger)
            {
                if (IsYellowOn())
                    LEDYellowOff();
                else
                    LEDYellowOn();
            }

        }
    }
}
